﻿using System;
using System.Collections.Generic;
using EndToEndSilverlightDemo;
using EndToEndSilverlightDemo.NorthWindServiceReference;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EndtoEndSilverlightDemo.Tests
{
    [TestClass]
    public class ProductSearchControllerSpec : SilverlightTest
    {
        [TestMethod]
        public void Should_set_root_visual_when_run_is_called()
        {
            IProductSearchView view = new ProductSearchViewStub();
            IApplication application = new SilverlightApplicationStub();
            IProductSearchService searchService=new ProductSearchServiceStub();
            IApplicationSettings settingsService=new ApplicationSettingsStub();
            
            IProductSearchController controller = new ProductSearchController(view,application,searchService,settingsService);
            

            controller.Run();


            Assert.AreEqual(application.RootVisual, view);
        }
        [TestMethod]
        public void When_the_settings_exist_the_controller_should_set_the_products_on_the_grid()
        {
            ProductSearchViewStub view = new ProductSearchViewStub();
            IApplication application = new SilverlightApplicationStub();
            IProductSearchService searchService = new ProductSearchServiceStub();
            IApplicationSettings settingsService = new ApplicationSettingsStub();

            settingsService.LastProductSearchResults = new Product[0];

            IProductSearchController controller = new ProductSearchController(view, application, searchService, settingsService);

            controller.Run();

            Assert.AreEqual(view.ProductItems, settingsService.LastProductSearchResults);
        }
      
        [TestMethod]
        public void When_the_search_button_on_view_is_clicked_the_search_service_should_be_called()
        {
            ProductSearchViewStub view = new ProductSearchViewStub();
            IApplication application = new SilverlightApplicationStub();
            ProductSearchServiceStub searchService = new ProductSearchServiceStub();
            IApplicationSettings settingsService = new ApplicationSettingsStub();

            settingsService.LastProductSearchResults = new Product[0];

            IProductSearchController controller = new ProductSearchController(view, application, searchService, settingsService);

            view.FireSearchButtonClicked("searchString");

            Assert.IsTrue(searchService.SearchWasCalled);
            Assert.AreEqual("searchString",searchService.SearchString);
            
        }

        [TestMethod]
        public void When_the_product_data_is_returned_the_product_list_should_be_bound_to_the_view()
        {
            ProductSearchViewStub view = new ProductSearchViewStub();
            IApplication application = new SilverlightApplicationStub();
            ProductSearchServiceStub searchService = new ProductSearchServiceStub();
            IApplicationSettings settingsService = new ApplicationSettingsStub();

            var products = new Product[]{new Product(){Discontinued=false,CategoryID=0,ProductID=0,ProductName="product name",QuantityPerUnit="12",ReorderLevel=10}};

            IProductSearchController controller = new ProductSearchController(view, application, searchService, settingsService);

            controller.GetProductsCompleted(null, new EventArgs<IEnumerable<Product>>(products));

            Assert.AreEqual(view.ProductItems, products);

        }
    }
}
